package pl.edu.pw.perg.compinfodb.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import pl.edu.pw.perg.compinfodb.entities.ComplexElement;
import pl.edu.pw.perg.compinfodb.entities.PhysicalElement;
import pl.edu.pw.perg.compinfodb.entities.VirtualElement;

public class ComplexElementDAO extends GenericSpringDAO<ComplexElement>
		implements IComplexElementDAO {

	public ComplexElement getComplexByVirtual(VirtualElement ve) {
		// Session s = getSession();
		List<ComplexElement> lst = findByCriteria(Restrictions.eq("veId", ve
				.getId()));
		if (lst.size() > 1)
			throw new IllegalStateException(
					"Not unique ComplexElement for VirtualElement with id: "
							+ ve.getId());
		if (lst.size() == 0)
			return null;
		return (ComplexElement) lst.get(0);
	}

	public ComplexElement getComplexByPhysical(PhysicalElement pe) {
		// Session s = getSession();
		List<ComplexElement> lst = findByCriteria(Restrictions.eq("peId", pe
				.getId()));
		if (lst.size() > 1)
			throw new IllegalStateException(
					"Not unique ComplexElement for PhysicalElement with id: "
							+ pe.getId());
		if (lst.size() == 0)
			return null;
		return (ComplexElement) lst.get(0);
	}

	@Override
	public void deleteAll() {
		Query q = getSession().createQuery("delete from ComplexElement");
		q.executeUpdate();
	}
}
